//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System;
using System.IO;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Validation.Properties;

namespace EntLibContrib.Validation.Configuration
{
	/// <summary>
	/// Externally Configured Object Validator Data
	/// </summary>
	[ConfigurationElementType(typeof(ExternallyConfiguredObjectValidatorData))]
	public class ExternallyConfiguredObjectValidatorData : ValidatorData
	{
		#region Constants
		private const string TargetRuleSetPropertyName = "targetRuleSet";
		private const string TargetConfigurationFilePropertyName = "targetConfigFile";
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the target ruleSet.
		/// </summary>
		/// <value>The target ruleSet.</value>
		[ConfigurationProperty(TargetRuleSetPropertyName)]
		public string TargetRuleSet
		{
			get { return (string)this[TargetRuleSetPropertyName]; }
			set { this[TargetRuleSetPropertyName] = value; }
		}

		/// <summary>
		/// Gets or sets the target configuration file.
		/// </summary>
		/// <value>The target configuration file.</value>
		[ConfigurationProperty(TargetConfigurationFilePropertyName, IsRequired = true)]
		public string TargetConfigurationFile
		{
			get { return (string)this[TargetConfigurationFilePropertyName]; }
			set { this[TargetConfigurationFilePropertyName] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectValidatorData"/> class.
		/// </summary>
		public ExternallyConfiguredObjectValidatorData()
		{ }

		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectValidatorData"/> class.
		/// </summary>
		/// <param name="name">The name of the element.</param>
		/// <param name="type">The <see cref="P:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.NameTypeConfigurationElement.Type"/> that this element is the configuration for.</param>
		protected ExternallyConfiguredObjectValidatorData(string name, Type type)
			: base(name, type)
		{ }

		/// <summary>
		/// Initializes a new instance of the <see cref="ExternallyConfiguredObjectValidatorData"/> class.
		/// </summary>
		/// <param name="name">The name of the element.</param>
		public ExternallyConfiguredObjectValidatorData(string name)
			: base(name, typeof(ExternallyConfiguredObjectValidator))
		{
			Name = name;
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Creates the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/> described by the configuration object.
		/// </summary>
		/// <param name="targetType">The type of object that will be validated by the validator.</param>
		/// <returns>
		/// The created <see cref="T:Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/>.
		/// </returns>
		/// <remarks>This operation must be overriden by subclasses.</remarks>
		protected override Validator DoCreateValidator(Type targetType)
		{
			if (!File.Exists(TargetConfigurationFile)) throw new ConfigurationErrorsException(Resources.ExternalConfigurationFileNotFound);

			return new ExternallyConfiguredObjectValidator(TargetRuleSet, TargetConfigurationFile);
		}
		#endregion
	}
}
